package com.white.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.white.bean.Comment;
import com.white.service.CommentService;
import com.white.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class CommentController {

    @Autowired
    CommentService commentService;

    @Autowired
    Utils utils;
    @PostMapping("/comment")
    @ResponseBody
    public boolean comment(@RequestParam("articleId") Long articleId,
                       @CookieValue(UserController.LOGIN_USER_ID) Long userId,
                       @RequestParam("content") String content) {
        Comment comment = new Comment(articleId, userId, content);
        try {
            if (!commentService.save(comment)) {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    @GetMapping("/index")
    public String index() {
        return "index";
    }



    @GetMapping("/getCommentPage/{page}/{limit}")
    @ResponseBody
    public Object getCommentPage(@PathVariable("page") int page,
                              @PathVariable("limit") int limit) {
        Page<Comment> commentPage = new Page<>(page, limit);
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 0);
        /*查询审核状态为0的评论信息*/
        return commentService.page(commentPage, queryWrapper);
    }


    @PostMapping("/updateComment")
    @ResponseBody
    public void updateComment(@RequestParam("item") String item) {
        Comment comment = utils.jsonToEntry(Comment.class, item);
        commentService.updateById(comment);
    }

    @GetMapping("/deleteCommentById/{id}")
    @ResponseBody
    public void deleteCommentById(@PathVariable("id") Long id) {
        commentService.removeById(id);
    }

    @GetMapping("/approveCommentById/{id}")
    @ResponseBody
    public void approveCommentById(@PathVariable("id") Long id) {
        Comment comment = new Comment();
        comment.setCommentId(id);
        comment.setStatus(1);
        commentService.updateById(comment);
    }
}
